﻿#include <stdio.h>
#include <easyx.h>
#include <conio.h>
int main()
{
	Game:
	initgraph(800, 600);
	setorigin(400, 300);
	setaspectratio(1, -1);
	setbkcolor(RGB(164, 255, 60));
	cleardevice();//清空窗体

	int x=0, y=0;
	int vx = 5; int vy = 5;
	int r = 40;
	int barLeft = -150;
	int barTop = -280;
	int barRight = 150;
	int barBottom = -300;
	while (1)
	{
		cleardevice();
		solidcircle(x, y, r);//绘制小球
		solidrectangle(barLeft, barTop, barRight, barBottom);
		Sleep(40);
		
		if (y >= 300 - r)
		{
			vy = -vy;
		}
		if (x >= 400 - r || x <= -400 + r)
		{
			vx = -vx;
		}
		x += vx;
		y += vy;
		if(_kbhit()!=0)
		{
			char c = _getch();
			if (c == 'a')
			{
				if (barLeft > -400)
				{
					barLeft -= 20;
					barRight -= 20;
				}
			}
			if (c == 'd')
			{
				if (barRight < 400)
				{
					barLeft += 20;
					barRight += 20;
				}
			}
		}
		if (barLeft <= x && x <= barRight && y <= barTop + r)
		{
			vy = -vy;
		}
		if (y <= -300)
		{
			goto Game;
		}
		
	}

	getchar();
	closegraph();
	return 0;
}